package com.maoyan.quickdevelop.system.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.maoyan.quickdevelop.common.core.domain.DqLike;
import com.maoyan.quickdevelop.common.utils.StringUtils;
import com.maoyan.quickdevelop.common.core.domain.DqUser;
import com.maoyan.quickdevelop.system.mapper.DqLikeMapper;
import com.maoyan.quickdevelop.system.mapper.DqUserMapper;
import com.maoyan.quickdevelop.system.service.IDqGivelikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 猫颜
 * @date 2021年07月24日 下午3:19
 * 类的作用：TODO
 */
@Transactional
@Service
@Slf4j
public class IDqGivelikeServiceImpl implements IDqGivelikeService {
    //    @Autowired
//    private RedisTemplate redisTemplate;
    @Autowired
    private DqUserMapper dqUserMapper;
    @Autowired
    private DqLikeMapper dqLikeMapper;
    QueryWrapper<DqLike> queryWrapper = new QueryWrapper<>();


//    String key = "";

    @Override
    public int giveALike(Long dqArticleId) {
        Long dqUserId = StpUtil.getLoginIdAsLong();
        DqLike dqLike = new DqLike();
        dqLike.setGiveLikeUserId(dqUserId);
        dqLike.setLikeArticleId(dqArticleId);
        dqLike.setCreateTime(new Date());
        dqLike.setUpdateTime(new Date());
        dqLike.setStatus("0");
        return dqLikeMapper.insert(dqLike);

    }

    @Override
    public int unlike(Long dqArticleId) {

        Long dqUserId = StpUtil.getLoginIdAsLong();
        DqLike dqLike = new DqLike();
        dqLike.setGiveLikeUserId(dqUserId);
        dqLike.setLikeArticleId(dqArticleId);
        PageHelper.startPage(1, 5);
        queryWrapper.lambda().eq(StringUtils.isNotNull(dqLike.getLikeArticleId()), DqLike::getLikeArticleId, dqLike.getLikeArticleId())
                .eq(StringUtils.isNotNull(dqLike.getGiveLikeUserId()), DqLike::getGiveLikeUserId, dqLike.getGiveLikeUserId());
        List<DqLike> dqLikes = dqLikeMapper.selectList(queryWrapper);
        queryWrapper.clear();
        for (DqLike like : dqLikes) {
            dqLikeMapper.deleteById(like.getLikeId());
        }
        return 1;
    }

    @Override
    public int checkStatusForDqArticle(Long dqArticleId) {
        Long dqUserId = StpUtil.getLoginIdAsLong();
        DqLike dqLike = new DqLike();
        dqLike.setGiveLikeUserId(dqUserId);
        dqLike.setLikeArticleId(dqArticleId);
        PageHelper.startPage(1, 5);
        queryWrapper.lambda().eq(StringUtils.isNotNull(dqLike.getLikeArticleId()), DqLike::getLikeArticleId, dqLike.getLikeArticleId())
                .eq(StringUtils.isNotNull(dqLike.getGiveLikeUserId()), DqLike::getGiveLikeUserId, dqLike.getGiveLikeUserId());
        List<DqLike> dqLikes = dqLikeMapper.selectList(queryWrapper);
        queryWrapper.clear();
        return dqLikes.size() > 0 ? 1 : 0;

    }

    @Override
    public int getLikeNumberInDqArticle(Long dqArticleId) {

        DqLike dqLike = new DqLike();
        dqLike.setLikeArticleId(dqArticleId);
        queryWrapper.lambda().eq(StringUtils.isNotNull(dqLike.getLikeArticleId()), DqLike::getLikeArticleId, dqLike.getLikeArticleId());
        List<DqLike> dqLikes = dqLikeMapper.selectList(queryWrapper);
        queryWrapper.clear();
        return dqLikes.size();
    }

    @Override
    public List<DqUser> likedDqUserInfoInDqArticle(int pageNum, int pageSize, Long dqArticleId) {

        DqLike dqLike = new DqLike();
        dqLike.setLikeArticleId(dqArticleId);
        PageHelper.startPage(pageNum, pageSize);
        queryWrapper.lambda().eq(StringUtils.isNotNull(dqLike.getLikeArticleId()), DqLike::getLikeArticleId, dqLike.getLikeArticleId());
        List<DqLike> dqLikes = dqLikeMapper.selectList(queryWrapper);
        queryWrapper.clear();
        List<DqUser> list = new ArrayList<>();
        for (DqLike like : dqLikes) {
            DqUser dqUser = dqUserMapper.selectById(like.getGiveLikeUserId());
            list.add(dqUser);
        }
        return list;
    }


}
